home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / stereo / GL_5.2 / glxhelper.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.4 KB  |  182 lines

  1. /*
  2.  * Copyright (C) 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  * glxhelper.c:
  19.  *
  20.  *   This file provides a helper function "GLXCreateWindow", which does
  21.  * all the necessary magic to create an X window suitable for GL drawing 
  22.  * to take place within.   see the definition of GLXCreateWindow for a 
  23.  * description of how to call it.
  24.  */
  25.  
  26. #include    <X11/Xlib.h>
  27. #include    <X11/Xutil.h>
  28. #include    <gl/glws.h>  
  29. #include    "glxhelper.h"
  30.  
  31. char *typeToName[] = {
  32.     "color index single buffer",
  33.     "color index double buffer",
  34.     "rgb single buffer",
  35.     "rgb double buffer",
  36. };
  37.  
  38. /*
  39.  * Dorky little helper function used to build up a GLXconfig array.
  40.  */
  41.  
  42. static void set_entry (GLXconfig* ptr, int b, int m, int a)
  43. {
  44.     ptr->buffer = b;
  45.     ptr->mode = m;
  46.     ptr->arg = a;
  47. }
  48.  
  49. /*
  50.  * GLXCreateWindow(dpy, parent, x, y, w, h, boderWidth, type)
  51.  *
  52.  * Return value is the X window id of the newly created window.
  53.  *
  54.  * Arguments are:
  55.  *    dpy        The X "Display*" returned by XOpenDisplay
  56.  *    parent        The parent of the newly created window,
  57.  *            a typical value for this is
  58.  *            RootWindow(dpy, DefaultScreen(dpy))
  59.  *    x,y        The location of the window to be created,
  60.  *            y coordinate is measured from the top down.
  61.  *    w,h        size of the new window
  62.  *    borderWidth    the X border size for this window, should probably
  63.  *            be zero.
  64.  *    type        the GLXWindowType (see glxhelper.h) desribing the
  65.  *            typer of GL drawing to be done in this window
  66.  */
  67. Window GLXCreateWindow(Display* dpy, Window parent, int x, int y, int w, int h,
  68.                int borderWidth, GLXWindowType type)
  69. {
  70.     GLXconfig params[50];
  71.     GLXconfig* next;
  72.     GLXconfig* retconfig;
  73.     Colormap cmap = DefaultColormap(dpy, DefaultScreen(dpy));
  74.     XVisualInfo* vis;
  75.     XVisualInfo template;
  76.     XColor white;
  77.     XSetWindowAttributes cwa;
  78.     XWindowAttributes    pwa;
  79.     int i, nret;
  80.     Window win;
  81.  
  82.     /*
  83.      * This builds an array in "params" that describes for GLXgetconfig(3G)
  84.      * the type of GL drawing that will be done.
  85.      */
  86.     next = params;
  87.     switch (type) {
  88.       case GLXcolorIndexSingleBuffer:
  89.     set_entry(next++, GLX_NORMAL, GLX_RGB, FALSE);
  90.     set_entry(next++, GLX_NORMAL, GLX_DOUBLE, FALSE);
  91.     break;
  92.       case GLXcolorIndexDoubleBuffer:
  93.     set_entry(next++, GLX_NORMAL, GLX_RGB, FALSE);
  94.     set_entry(next++, GLX_NORMAL, GLX_DOUBLE, TRUE);
  95.     break;
  96.       case GLXrgbSingleBuffer:
  97.     set_entry(next++, GLX_NORMAL, GLX_RGB, TRUE);
  98.     set_entry(next++, GLX_NORMAL, GLX_DOUBLE, FALSE);
  99.     break;
  100.       case GLXrgbDoubleBuffer:
  101.     set_entry(next++, GLX_NORMAL, GLX_RGB, TRUE);
  102.     set_entry(next++, GLX_NORMAL, GLX_DOUBLE, TRUE);
  103.     break;
  104.     }
  105.     set_entry(next, 0, 0, 0); /* The input to GLXgetconfig is null terminated */
  106.  
  107.     /*
  108.      * Get configuration data for a window based on above parameters
  109.      * First we have to find out which screen the parent window is on,
  110.      * then we can call GXLgetconfig()
  111.      */
  112.     XGetWindowAttributes(dpy, parent, &pwa);
  113.     retconfig = GLXgetconfig(dpy, XScreenNumberOfScreen(pwa.screen), params);
  114.     if (retconfig == 0) {
  115.     printf("Sorry, can't support %s type of windows\n", typeToName[type]);
  116.     exit(-1);
  117.     }
  118.     /*
  119.      * The GL sets its own X error handlers, which aren't as informative
  120.      * when errors happen.  Calling XSetErrorHandler(0) here will
  121.      * reset back to the default Xlib version.
  122.      */
  123.     XSetErrorHandler(0);
  124.  
  125.     /*
  126.      * Scan through config info, pulling info needed to create a window
  127.      * that supports the rendering mode.
  128.      */
  129.     for (next = retconfig; next->buffer; next++) {
  130.     unsigned long buffer = next->buffer;
  131.     unsigned long mode = next->mode;
  132.     unsigned long value = next->arg;
  133.     switch (mode) {
  134.       case GLX_COLORMAP:
  135.         if (buffer == GLX_NORMAL) {
  136.         cmap = value;
  137.         }
  138.         break;
  139.       case GLX_VISUAL:
  140.         if (buffer == GLX_NORMAL) {
  141.         template.visualid = value;
  142.         template.screen = DefaultScreen(dpy);
  143.         vis = XGetVisualInfo(dpy, VisualScreenMask|VisualIDMask,
  144.                       &template, &nret);
  145.         }
  146.         break;
  147.     }
  148.     }
  149.  
  150.     /*
  151.      * Create the window
  152.      */
  153.     cwa.colormap = cmap;
  154.     cwa.border_pixel = 0;  /* Even if we don't use it, it must be something */
  155.     win = XCreateWindow(dpy, parent, x, y, w, h,
  156.                  borderWidth, vis->depth, InputOutput, vis->visual,
  157.                  CWColormap|CWBorderPixel, &cwa);
  158.  
  159.     /*
  160.      * Rescan configuration info and find window slot that getconfig
  161.      * provided.  Fill it in with the window we just created.
  162.      */
  163.     for (next = retconfig; next->buffer; next++) {
  164.     if ((next->buffer == GLX_NORMAL) && (next->mode == GLX_WINDOW)) {
  165.         next->arg = win;
  166.         break;
  167.     }
  168.     }
  169.  
  170.     /*
  171.      * Now "retconfig" contains all the information the GL needs to
  172.      * configure the window and its own internal state.
  173.      */
  174.     i = GLXlink(dpy, retconfig);
  175.     if (i < 0) {
  176.     printf("GLXlink returned %d\n", i);
  177.     exit(-1);
  178.     }
  179.  
  180.     return win;
  181. }
  182.